<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" :class="{dark: $store.state.app.menuTheme==='dark'}" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="captcha">
                            <Input type="text" size="large" v-model="form.captcha" placeholder="请输入验证码">
                                <span slot="prepend">
                                    <Icon size="14" type="code-working"></Icon>
                                </span>
                                <span slot="append" @click="reCode"><img class="v-code" :src="codeSrc"></span>
                            </Input>
                        </FormItem>

                        <FormItem>
                            <Button :loading="loading"  @click="handleSubmit" type="primary" long>
                                <span v-if="!loading">登录</span>
                                <span v-else>登录中...</span>
                            </Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
// import Cookies from 'js-cookie';
// import CryptoJS from 'crypto-js';
export default {
    data () {
        return {
            loading: false,
            codeSrc: '',
            radomStr: '',
            form: {
                userName: '',
                password: '',
                captcha: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ],
                captcha: [
                    {required: true, message: '验证码不能为空', trigger: 'blur'}
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$post('/login', {
                        username: this.form.userName,
                        password: this.form.password,
                        captcha: this.form.captcha,
                        uuid: this.radomStr
                    }, 'json')
                        .then(res => {
                            localStorage.setItem('token', res.token);
                            localStorage.setItem('access', res.access);
                            localStorage.setItem('admin', JSON.stringify(res.user));
                            localStorage.setItem('lastLoginTime', new Date().toLocaleString());
                            if (typeof window.returnCitySN === 'object' && Object.prototype.toString.call(window.returnCitySN) === '[object Object]') {
                                localStorage.setItem('address', window.returnCitySN['cip']);
                            }
                            this.$store.commit('setAvator', res.user.headImg);
                            this.loading = false;
                            this.$Message.success('登录成功');
                            this.$router.push({
                                name: 'home_index'
                            });
                        })
                        .catch(error => {
                            this.loading = false;
                            this.$Message.error(error.message);
                        });
                }
            });
        },
        reCode () {
            this.buildCdoe();
        },
        buildCdoe () {
            this.radomStr = new Date().getTime();
            this.codeSrc = this.$store.state.baseURL + '/captcha.jpg?uuid=' + this.radomStr;
        }
    },
    created () {
        this.buildCdoe();
    }
};
</script>

<style>

</style>
